//created by wzq 组件库
//需要：css/components.css
(function($){

/** =========================================================表情选择框类========================================================= **/

	var availableEmotions = ["thumbsup","thumbsdown","angry","anguished","astonished","blush","confounded","confused","cry","fearful","flushed"];

	function EmotionPicker(emojiFolder){
		var $dom = $("<div class='emotionPicker'></div>");
		var self = this;
		
		$dom.click(function(e){
			e.stopPropagation();
			if(e.target !== $dom[0])//不等于自身表示点到的是表情
			{
				var appendTxt = ":" + $(e.target).data("emotion") + ":";
				if(self.$bindArea.length > 0)
				{
					self.$bindArea.val(self.$bindArea.val() + appendTxt);
				}
				self.hide();
			}
		});
		
		$('body').append($dom);
		var len = availableEmotions.length, $eItem;
		for(var i=0;i<len;i++)
		{
			$eItem = $("<img src='" + emojiFolder + "/" + availableEmotions[i] + ".png' class='emotionItem' data-emotion='" + availableEmotions[i] + "'/>");
			$dom.append($eItem);
		}
		
		
		self.$dom = $dom;
		self.click2Hide = function(){self.hide();};
		self.hide();
	}

	EmotionPicker.prototype.show = function($targetDom){
		var pos = $targetDom.offset(), w = $targetDom.outerWidth(), self = this;
		this.$dom.css({top:pos.top, left:pos.left+w});
		this.$dom.show();
		$("body").on("click",self.click2Hide);
		this.isHidden = false;
	};

	EmotionPicker.prototype.hide = function(){
		this.$dom.hide();
		$("body").off("click",this.click2Hide);
		this.isHidden = true;
	};

	EmotionPicker.prototype.toggle = function(targetDom){
		this.isHidden ? this.show(targetDom) : this.hide();
	};

	window.EmotionPicker = EmotionPicker;

	
/** =========================================================回到顶部按钮========================================================= **/

	function ToTopButton(){
		var $dom = $('<div class="toTopButton" title="回到顶部" onclick="toTop()"></div>');
		$("body").append($dom);
		$dom.hide();
		$(document).scroll(function(){
			var scrollTop = $(document).scrollTop();
			scrollTop > 20 ? $dom.show() : $dom.hide();
		});
	};
	ToTopButton.prototype.toTop = function(){
		$('html, body').animate({scrollTop: 0},700);
	};
	window.ToTopButton = ToTopButton;

})(jQuery);
